using Sage.Http.Authentication;
using Sage.Http.Core;

namespace Sage.Http.Models
{
    /// <summary>
    /// 封装HTTP请求的各种配置选项，简化复杂参数传递
    /// </summary>
    public class RequestOptions
    {
        /// <summary>
        /// 请求头集合
        /// </summary>
        public Dictionary<string, string> Headers { get; set; } = [];

        /// <summary>
        /// 查询参数集合
        /// </summary>
        public Dictionary<string, object> QueryParameters { get; set; } = [];

        /// <summary>
        /// 认证提供者
        /// </summary>
        public IAuthenticationProvider? AuthProvider { get; set; }

        /// <summary>
        /// 请求超时时间
        /// </summary>
        public TimeSpan? Timeout { get; set; }

        /// <summary>
        /// 上传进度报告接口
        /// </summary>
        public IProgress<float>? UploadProgress { get; set; }

        /// <summary>
        /// 下载进度报告接口
        /// </summary>
        public IProgress<float>? DownloadProgress { get; set; }

        /// <summary>
        /// 取消令牌
        /// </summary>
        public CancellationToken CancellationToken { get; set; } = CancellationToken.None;

        /// <summary>
        /// 将所有配置选项应用到请求构建器
        /// </summary>
        /// <param name="builder">HTTP请求构建器</param>
        public void ApplyTo(HttpRequestBuilder builder)
        {
            ArgumentNullException.ThrowIfNull(builder);

            foreach (var header in Headers)
                builder.WithHeader(header.Key, header.Value);

            foreach (var param in QueryParameters)
                builder.WithQuery(param.Key, param.Value);

            if (AuthProvider != null)
                builder.WithAuthentication(AuthProvider);

            if (Timeout.HasValue)
                builder.WithTimeout(Timeout.Value);

            if (UploadProgress != null)
                builder.WithProgress(UploadProgress);
        }
    }
}
